package bb.lanxing.activity.bluetooth;

import androidx.annotation.NonNull;
import java.io.File;
import java.util.List;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import androidx.appcompat.widget.PopupMenu;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import bb.lanxing.R;
import bb.lanxing.adapter.DeviceSyncAdapter;
import bb.lanxing.adapter.DeviceSyncAdapter2;
import bb.lanxing.databinding.sprint.DeviceSyncItem;
import bb.lanxing.devices.sync.XZSyncHelper;
import bb.lanxing.lib.devices.core.sync.DeviceFile;
import bb.lanxing.lib.devices.core.sync.DeviceFileStatus;
import bb.lanxing.lib.devices.core.sync.DeviceSyncListener;
import bb.lanxing.lib.devices.core.sync.SyncManager;
import bb.lanxing.util.AppFileUtils;
import bb.lanxing.util.RecyclerViewItemClickHelper;
import in.srain.cube.views.ptr.PtrUIHandlerHook;

public class GenericSyncActivity2 extends BaseSyncActivity implements DeviceSyncListener,
        RecyclerViewItemClickHelper.OnItemLongClickListener, DeviceSyncAdapter.EventListener, RecyclerViewItemClickHelper.OnItemClickListener {
    public static final String EXTRA_SYNC_MANAGER_NAME = "extra_sync_manager";
    private DeviceSyncAdapter adapter;
    private RecyclerViewItemClickHelper clickHelper;
    private RecyclerView recyclerView;
    private SwipeRefreshLayout refreshLayout;

    @Override
    public void onDeviceStatus(byte b) {
    }

    @Override
    public void onItemClick(RecyclerView.ViewHolder viewHolder) {
    }

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_device_file_sync);
        getWindow().addFlags(128);
        initViews();
        bindSyncService();
    }

    protected void initViews() {
        setupActionBar(true);
        setTitle(R.string.mine_title_history);
        this.recyclerView = (RecyclerView) findViewById(R.id.rv_list);
        this.refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
        this.recyclerView.addItemDecoration(new DividerItemDecoration(this, 1));
        this.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        this.clickHelper = new RecyclerViewItemClickHelper(this, this);
        RecyclerViewItemClickHelper recyclerViewItemClickHelper = new RecyclerViewItemClickHelper(this, this);
        this.clickHelper = recyclerViewItemClickHelper;
        recyclerViewItemClickHelper.attachToRecyclerView(this.recyclerView);
        refreshLayout.setOnRefreshListener(()->{
            if (GenericSyncActivity2.this.mSyncManager != null) {
                GenericSyncActivity2.this.mSyncManager.readFileList();
            }
        });
    }
    public DeviceSyncAdapter getAdapter() {
        return this.adapter;
    }

    protected DeviceSyncAdapter createDeviceSyncAdapter() {
        return new DeviceSyncAdapter2();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_device_sync, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem menuItem) {
        if (menuItem.getItemId() == R.id.menu_item_sync_all) {
            if (this.mSyncManager == null) {
                return true;
            }
            this.mSyncManager.sync();
            return true;
        }
        return super.onOptionsItemSelected(menuItem);
    }

    @Override
    public void onItemLongClick(RecyclerView.ViewHolder viewHolder) {
        final DeviceFile deviceFileByPosition = this.adapter.getDeviceFileByPosition(viewHolder.getAdapterPosition());
        if (deviceFileByPosition == null) {
            return;
        }
        final long id = deviceFileByPosition.getId();
        PopupMenu popupMenu = new PopupMenu(this, viewHolder.itemView);
        popupMenu.inflate(R.menu.menu_device_sync_item);
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                if (menuItem.getItemId() == R.id.device_sync_re_sync_record) {
                    deleteFile(id, deviceFileByPosition.getPath());
                    adapter.getDeviceSyncItem(id).setStatus(mSyncManager.getSyncState(id));
                    adapter.notifyDataSetChanged();
                    return true;
                } else if (menuItem.getItemId() != R.id.device_sync_delete_record) {
                    return true;
                } else {
                    mSyncManager.delete(id);
                    return true;
                }
            }
        });
        popupMenu.show();
    }

    public void deleteFile(long j, String str) {
        if (str != null) {
            try {
                new File(str).delete();
            } catch (Exception e) {
                e.printStackTrace();
            }
            this.mSyncManager.sync(j);
        }
    }

    @Override
    public void onSync(DeviceFile deviceFile) {
        if (this.mSyncManager == null || deviceFile == null || AppFileUtils.isFileExists(deviceFile.getPath())) {
            return;
        }
        this.mSyncManager.sync(deviceFile.getId());
    }

    @Override
    public void onReadFile(List<DeviceFile> list) {
        this.refreshLayout.setRefreshing(false);
        if (list == null) {
            toast(R.string.str_sync_get_files_fail);
            return;
        }
        if (this.adapter == null) {
            DeviceSyncAdapter createDeviceSyncAdapter = createDeviceSyncAdapter();
            this.adapter = createDeviceSyncAdapter;
            createDeviceSyncAdapter.setEventListener(this);
            this.recyclerView.setAdapter(this.adapter);
        }
        this.adapter.setSyncManager(this.mSyncManager);
        this.adapter.setDeviceFiles(list);
        this.adapter.notifyDataSetChanged();
    }

    @Override
    public void onProgressUpdate(DeviceFile deviceFile, float f) {
        DeviceSyncAdapter deviceSyncAdapter = this.adapter;
        DeviceSyncItem deviceSyncItem = deviceSyncAdapter != null ? deviceSyncAdapter.getDeviceSyncItem(deviceFile.getId()) : null;
        if (deviceSyncItem != null) {
            if (this.mSyncManager != null) {
                deviceSyncItem.setStatus(this.mSyncManager.getSyncState(deviceFile.getId()));
            }
            deviceSyncItem.setProgress(f);
        }
    }

    @Override
    public void onSyncStatus(DeviceFile deviceFile, int i, String str) {
        DeviceSyncAdapter deviceSyncAdapter = this.adapter;
        DeviceSyncItem deviceSyncItem = deviceSyncAdapter != null ? deviceSyncAdapter.getDeviceSyncItem(deviceFile.getId()) : null;
        if (deviceSyncItem != null) {
            deviceSyncItem.setStatus(i);
        }
        DeviceFileStatus vStatus = DeviceFileStatus.valueOf(i);
        if (vStatus != null && this.mSyncManager != null) {
            if (!this.mSyncManager.isSynchronisingWithMultiFiles()) {
                if (vStatus == DeviceFileStatus.STATUS_SYNC_FAIL) {
                    toast(R.string.str_sync_failed);
                } else if (vStatus == DeviceFileStatus.STATUS_SYNCED) {
                    toast(R.string.str_sync_success);
                }
            }
            if (vStatus == DeviceFileStatus.STATUS_DELETED) {
                toast(R.string.toast_delete_successful);
            } else if (vStatus == DeviceFileStatus.STATUS_DELETE_FAIL) {
                toast(R.string.toast_delete_failed);
            }
        }
    }

    @Override
    public void onAttachedToSyncManager(SyncManager syncManager) {
        super.onAttachedToSyncManager(syncManager);
        if (this.mSyncManager != null) {
            this.mSyncManager.registerSyncListener(this);
            if (isSynchronising()) {
                this.mSyncManager.readFileList();
            } else {
                this.refreshLayout.post(new PtrUIHandlerHook() {
                    @Override
                    public void run() {
                        if (GenericSyncActivity2.this.mSyncManager != null) {
                            GenericSyncActivity2.this.mSyncManager.readFileList();
                        }
                    }
                });
            }
        }
    }

    @Override
    public void unbindSyncService(boolean z) {
        SyncManager syncManager;
        if (this.mSyncHelper != null && (syncManager = this.mSyncHelper.getSyncManager()) != null) {
            syncManager.unregisterSyncListener(this);
        }
        super.unbindSyncService(z);
    }

    @Override
    public void onDetachedFromSyncManager(SyncManager syncManager) {
        super.onDetachedFromSyncManager(syncManager);
        if (syncManager != null) {
            syncManager.unregisterSyncListener(this);
        }
    }

    @Override
    protected String getSyncManagerName() {
        String stringExtra = getIntent().getStringExtra(EXTRA_SYNC_MANAGER_NAME);
        return stringExtra == null ? XZSyncHelper.DISCOVERY_SYNC_MANAGER_NAME : stringExtra;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RecyclerViewItemClickHelper recyclerViewItemClickHelper = this.clickHelper;
        if (recyclerViewItemClickHelper != null) {
            recyclerViewItemClickHelper.destroy();
        }
    }
}
